package com.tanhua.server.controller;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.tanhua.server.service.QuanZiService;
import com.tanhua.server.vo.CommentVo;
import com.tanhua.server.vo.PageResult;
import com.tanhua.server.vo.QuanZiVo;
import com.tanhua.server.vo.VisitorsVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;


@RestController
@RequestMapping("movements")
public class QuanZiController {
    @Autowired
    private  QuanZiService quanZiService;
    @GetMapping
   public PageResult querypublishlist(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                      @RequestParam(value = "pagesize",defaultValue = "10")Integer pagesize){
    return quanZiService.querypublishlist(page,pagesize);
   }
   @PostMapping
   public ResponseEntity<Void>savepublish(@RequestParam("textContent")String textContent,
                                          @RequestParam(value = "imageContent",required = false) MultipartFile[] multipartFile,
                                           @RequestParam(value ="location" )String location,
                                            @RequestParam(value ="longitude")String longitude,
                                           @RequestParam(value = "latitude")String latitude){
       try {
           String publishId=quanZiService.savepublish(textContent,multipartFile,location,longitude,latitude);
           if(StringUtils.isNotEmpty(publishId)){
               return ResponseEntity.ok(null);
           }
       } catch (Exception e) {
           e.printStackTrace();
       }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
   }
    @GetMapping("recommend")
    public PageResult queryRecommendPublishList(@RequestParam(value = "page",defaultValue = "1")Integer page,
                                                @RequestParam(value = "pagesize",defaultValue = "10")Integer pagesize){
      return quanZiService.queryRecommendPublishList(page,pagesize);
    }
    @GetMapping("{id}/like")
    public ResponseEntity<Long> likeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount=  quanZiService.likeComment(publishId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return  ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("{id}/dislike")
    public  ResponseEntity<Long> dislikeComment(@PathVariable("id")String publishId){
        try {
            Long likeCount= quanZiService.dislikeComment(publishId);
            if(ObjectUtil.isNotEmpty(likeCount)){
                return  ResponseEntity.ok(likeCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("{id}/love")
    public ResponseEntity<Long> loveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount=  quanZiService.loveComment(publishId);
            if(ObjectUtil.isNotEmpty(loveCount)){
                return  ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    @GetMapping("{id}/unlove")
    public  ResponseEntity<Long> disloveComment(@PathVariable("id")String publishId){
        try {
            Long loveCount= quanZiService.disloveComment(publishId);
            if(ObjectUtil.isNotEmpty(loveCount)){
                return  ResponseEntity.ok(loveCount);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
     @GetMapping("{id}")
     public ResponseEntity<QuanZiVo>queryById(@PathVariable("id")String publishId){
         try {
             QuanZiVo quanZiVo = quanZiService.queryById(publishId);
             if(ObjectUtil.isNotEmpty(quanZiVo)){
                 return ResponseEntity.ok(quanZiVo);
             }
         } catch (Exception e) {
             e.printStackTrace();
         }return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
     }
    @GetMapping("all")
    public ResponseEntity<PageResult> queryAlbumList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                     @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "userId") Long userId) {
        try {
            PageResult pageResult = this.quanZiService.queryAlbumList(userId, page, pageSize);
            return ResponseEntity.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }

    /**
     * 谁看过我
     *
     * @return
     */
    @GetMapping("visitors")
    public ResponseEntity<List<VisitorsVo>> queryVisitorsList(){
        try {
            List<VisitorsVo> list = this.quanZiService.queryVisitorsList();
            return ResponseEntity.ok(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
}
